$(document).ready(function(){

	var html = "<ul data-role=\"listview\" data-inset=\"true\" data-filter=\"true\" data-filter-reveal=\"true\" data-filter-placeholder=\"Task list...\">";
	$.getJSON('/Server/gonogo.php', function(data) {
		var task;
		for (var i = 0; i < data.length; i++ ){
			task = data[i];
			//html += "<li><a href=\"www.google.fr\">" + task + "</a></li>";
			//html += "<li><a href=\"goNoGo.php?fileName=\">" + task + "</a></li>";
			html += "<li><a href=\"task.html?fileName="+task+"\" id=\"url\" data-ajax=\"false\">" + task + "</a></li>";
		}
		html += "</ul>";
		
		$('#content').html(html).trigger("create");
	});
	/*
	$('#fileName').val().click(function(){
		$.post(url,params)
	});*/
	
});

function loadXMLDoc(dname)
{
	if (window.XMLHttpRequest)
	{
		xhttp=new XMLHttpRequest();
	}
	else
	{
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.open("GET",dname,false);
	xhttp.send();
	return xhttp.responseXML;
}


function displayXMLDoc(dname)
{
	$('#Title').append(xmlDoc.getElementsByTagName("Title")[0].childNodes[0].nodeValue)
	$('#Sequence').append("Sequence : " + xmlDoc.getElementsByTagName("Sequence")[0].childNodes[0].nodeValue)
	$('#Date').append("Date : " + xmlDoc.getElementsByTagName("Date")[0].childNodes[0].nodeValue)
	$('#Name').append("Name : " + xmlDoc.getElementsByTagName("Name")[0].childNodes[0].nodeValue)
	var lineNumber = 0;
	for(var i = 0; i < xmlDoc.getElementsByTagName("Subtitle").length; i++){
		var currentSubtitle = xmlDoc.getElementsByTagName("Subtitle")[i];
		$('#list').append('<li data-role="list-divider" role="heading">' + currentSubtitle.childNodes[0].nodeValue + '</li>');
		for(var j = 0; j <  currentSubtitle.getElementsByTagName("Description").length; j++){
			var taskContent = currentSubtitle.getElementsByTagName("Description")[j].childNodes[0].nodeValue;
			var comments = "";
			if(currentSubtitle.getElementsByTagName("Comments")[j].childNodes[0]){
				comments = currentSubtitle.getElementsByTagName("Comments")[j].childNodes[0].nodeValue;
			}
			var toBeDone = "";
			var onGoing = "";
			var done = "";
			if(currentSubtitle.getElementsByTagName("Status")[j].childNodes[0]){
				if(currentSubtitle.getElementsByTagName("Status")[j].childNodes[0].nodeValue == 'To Be Done'){
					toBeDone = 'checked="checked"'
				}
				else if(currentSubtitle.getElementsByTagName("Status")[j].childNodes[0].nodeValue == 'On Going'){
					onGoing = 'checked="checked"';
				}
				else{
					done = 'checked="checked"';
				}
			}
			$('#list').append('<li data-theme="c">'+
				'<div class="elementFrom' + currentSubtitle.childNodes[0].nodeValue.trim() + '">' +
				'<fieldset data-role="controlgroup" data-type="horizontal">'+
				'<input id="toBeDone" name="' + lineNumber +'" value="To Be Done" type="radio"' + toBeDone + ' >' +
				'<label for="toBeDone">'+
				'To Be Done'+
				'</label>'+
				'<input id="onGoing" name="' + lineNumber +'" value="On Going" type="radio"' + onGoing + '>' +
				'<label for="onGoing">'+
				'On Going'+
				'</label>'+
				'<input id="done" name="' + lineNumber +'" value="Done" type="radio"' + done + '>'+
				'<label for="done">'+
				'Done'+
				'</label>'+
				'</fieldset>'+
				'<div class="taskDescription">' +
				taskContent +
				'</div>' +
				'<textarea name="" id="taskComment" placeholder="Comments">' +
				comments +
				'</textarea>' +
				'</div>' +
				'</li>');
			lineNumber++;
		}
	}
}

function saveXMLFile(){
	console.log('&lt');
	var fileName = 'Tasks/' + $('#filename').val() + '.xml';
	var fileContent = "";
	fileContent += '<DocumentGoNoGo>\n';
	fileContent += '    <Title>' + $('#Title').html().trim().replace(/</g, '&lt;').replace(/>/g, '&gt;') + '</Title>\n';
	fileContent += '    <Sequence>' + $('#Sequence').html().trim().replace("Sequence : ","").replace(/</g, '&lt;').replace(/>/g, '&gt;')  + '</Sequence>\n';
	fileContent += '    <Date>' + $('#Date').html().trim().replace("Date : ","").replace(/</g, '&lt;').replace(/>/g, '&gt;')  + '</Date>\n';
	fileContent += '    <Name>' + $('#Name').html().trim().replace("Name : ","").replace(/</g, '&lt;').replace(/>/g, '&gt;')  + '</Name>\n';
	for(var subList = 0; subList < $('li[role="heading"]').length; subList++){
		var subListName = $('li[role="heading"]')[subList].firstChild.data.trim();
		fileContent += '    <Subtitle>' + subListName + '\n';
		for(var taskCount = 0; taskCount < $('div[class="elementFrom' + subListName + '"]').length; taskCount++){
			fileContent += '        <Task>\n';
			var taskContent = $('div[class="elementFrom' + subListName + '"]')[taskCount];

			var taskDescriptionSize = taskContent.childNodes[1].firstChild.length;
			fileContent += '            <Description>' + taskContent.childNodes[1].firstChild.substringData(0,taskDescriptionSize).trim().replace(/</g, '&lt;').replace(/>/g, '&gt;')  + '</Description>\n';

			var taskStatus = "";
			if(taskContent.firstChild.firstChild.childNodes[0].firstChild.checked){
				taskStatus = taskContent.firstChild.firstChild.childNodes[0].firstChild.value.trim();
			}
			else if(taskContent.firstChild.firstChild.childNodes[1].firstChild.checked){
				taskStatus = taskContent.firstChild.firstChild.childNodes[1].firstChild.value.trim();
			}
			if(taskContent.firstChild.firstChild.childNodes[2].firstChild.checked){
				taskStatus = taskContent.firstChild.firstChild.childNodes[2].firstChild.value.trim();
			}
			fileContent += '            <Status>' + taskStatus + '</Status>\n';

			fileContent += '            <Comments>' + taskContent.childNodes[2].value.trim().replace(/</g, '&lt;').replace(/>/g, '&gt;')  + '</Comments>\n';

			fileContent += '        </Task>\n';
		}
		fileContent += '    </Subtitle>\n';
	}
	fileContent += '</DocumentGoNoGo>';
	$.post("/Server/saveFile.php", {function_name: 'putInFile', params:{filename: fileName, filedata: fileContent}},function(data){console.log(data)});
}

